#!/usr/bin/perl
# client.pl
use warnings;
use strict;

use POSIX qw(O_RDWR O_CREAT O_EXCL tmpnam);
use Sys::Hostname; # for 'hostname'

die "Simple anonymous FTP command line client\n".
    "Usage: $0 <server> <command>\n" unless scalar(@ARGV)>=2;

my ($ftp_server,@ftp_command)=@ARGV;

# get an open and unique temporary file
my $ftp_resultfile;
do {
	# generate a new temporary filename
	$ftp_resultfile = tmpnam();
	# O_EXCL ensures no other process successfully opens the same file
	sysopen FTP_RESULT, $ftp_resultfile, O_RDWR|O_CREAT|O_EXCL;
	# failure means something else opened this filename first, try again
} until (defined fileno(FTP_RESULT));

# run ftp client with autologin disabled (using -n)
if (open (FTP, "|ftp -n > $ftp_resultfile 2>&1")) {
	print "Client running, sending command\n";

	# command: open connection to server
	print FTP "open $ftp_server\n";
	# command: specify anonymous user and email as password
	my $email=getlogin.'@'.hostname;
	print FTP "user anonymous $email\n";
	# command: send command (interpolate list to space arguments)
	print FTP "@ftp_command\n";

	close FTP;
} else {
	die "Failed to run client: $!\n";
}

print "Command sent, waiting for response\n";
my @ftp_results = <FTP_RESULT>;
check_result(@ftp_results);
close FTP_RESULT;
unlink $ftp_resultfile;
print "Done\n";

sub check_result {
	return unless @_;

	print "Response:\n";
	# just print out the response for this example
	print "\t$_" foreach @_;
}